package
{
	import com.adobe.serialization.json.JSON;
	import com.athena.controller.Handle;
	import com.athena.controller.LoaderFile;
	import com.athena.Objects.ConfigObject;
	import com.athena.Objects.DailyObject;
	import com.athena.Objects.GameObject;
	import com.athena.Objects.LoginObject;
	import com.athena.Objects.PlayerObject;
	import com.athena.Objects.SystemSetting;
	import com.athena.socket.CustomSocket;
	import com.athena.utils.Util;
	import com.athena.view.ChatRoom;
	import com.athena.view.DashBoard;
	import com.athena.view.GameBoard;
	import com.cubeia.firebase.io.protocol.LeaveRequestPacket;
	import com.junkbyte.console.Cc;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.DataEvent;
	import flash.events.Event;
	
	/**
	 * ...
	 * @author chau
	 */
	[SWF(width="1024",height="600",backgroundColor="#CCCCCC",frameRate="30")]
	
	public class Main extends Sprite {
		private static var _instance:Main;
		private var _loginObject:LoginObject;
		private var _gameObject:GameObject = new GameObject(8005);
		private var _currPlayer:PlayerObject;
		private var _handle:Handle;
		private var _socket:CustomSocket;
		private var _dailyObject:DailyObject = new DailyObject();
		private var _config:ConfigObject = new ConfigObject();
		private var _loaderFile : LoaderFile;
		private var _dashBoard : DashBoard;
		private var _gameBoard : GameBoard;
		
		private var _chatRoom : ChatRoom;
		
		private var _sysSetting : SystemSetting;
		public function Main():void {

			Cc.config.commandLineAllowed = true // Enables full commandLine features
			Cc.config.tracing = true; // also send traces to flash's normal trace()
			Cc.config.maxLines = 2000; // change maximum log lines to 2000, default is 1000
			Cc.startOnStage(stage, "");
			
			if (stage)
				init();
			else
				addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event = null):void {
			removeEventListener(Event.ADDED_TO_STAGE, init);
			_instance = this;
			tabEnabled = false;
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP;
			
			tabChildren = false;
			
			
			_handle = new Handle();
			
			loadConfig();
		}
		
		public function responeChat(obj : Object) : void {
			if (obj.evt == "chattable") {
				
			}else {
				_chatRoom.addTextChat(obj);
			}
		}
		private function loadConfig():void {
			_loaderFile = new LoaderFile();
			_loaderFile.addEventListener(LoaderFile.LOADCONFIG, compl);
			_loaderFile.loadConfig();
			
			function compl(e:Event):void {
				_config.Config = _loaderFile.configData;
				_loaderFile.removeEventListener(LoaderFile.LOADCONFIG, compl);
				loadAsset();
			}
		}
		
		private function loadAsset() : void {
			_loaderFile.addEventListener(LoaderFile.LOAD_ASSET_PROCESS, process);
			_loaderFile.addEventListener(LoaderFile.LOAD_ASSET_COMPLETE, complete);
			_loaderFile.loadAssetFile();
			
			function process(e : DataEvent) : void {
				//Cc.log(e.data);
			}
			function complete(e:Event):void {
				_loaderFile.removeEventListener(LoaderFile.LOAD_ASSET_PROCESS, process);
				_loaderFile.removeEventListener(LoaderFile.LOAD_ASSET_COMPLETE, complete);
				login();
			}
		}
		
		private function login() : void {
			_loginObject = new LoginObject("229994221", "test1", "bacay");
			_loginObject.sessionId = "QjVtyt3qF0qAuUllH-W2SLRboPCNkmjnVEx9wXRWVLm9XDQtUuvWBJJVWku6hL8vD9Ro-7tEI6ism_oeQjycHt26dDmsttaIVzUNWMI9TnCWWCIBCOXsGnpH_evCa0flCAJwwXpz4Jq2yfQz8vnhScY0xuT5wKHqFj3Wr4Ym5YPFgUfZ8fXxbbj_VvtYG_vgdbvuskL1lbp123ofuXFYSjDGVFd-RUiF-HH4ziPxyL8cgmIV44hYV5W=";
			_socket = new CustomSocket("123.30.171.164", "4123");
		}
		public function displayDashBoard(data : String) : void {
			if (!_dashBoard)  {
				_chatRoom = new ChatRoom();
				_dashBoard = new DashBoard(_loaderFile.dash, data);
				addEventDashBoard();
				addChild(_dashBoard.movie);
			}
		}
		private function sendPlayNow(e  :Event) : void {
			_dashBoard.removeEventListener(DashBoard.CHOINGAY, sendPlayNow);
			var _ob : Object = new Object;
			_ob.evt = "searchT";
			_socket.sendService(JSON.encode(_ob));
		}
		private function addEventDashBoard() : void {
			_dashBoard.addEventListener(DashBoard.CHOINGAY, sendPlayNow);
		}
		
		private function sendExitTable(e : Event) : void {
			
			var leaveRequestPacket:LeaveRequestPacket = new LeaveRequestPacket();
			leaveRequestPacket.tableid = _gameObject.TABLEID;
			_socket.send(leaveRequestPacket);
		}
		public static function getInstance():Main {
			return _instance;
		}
		
		public function get loginObject():LoginObject {
			return _loginObject;
		}
		
		public function get gameObject():GameObject {
			return _gameObject;
		}
		
		public function get currPlayer():PlayerObject {
			return _currPlayer;
		}
		
		public function set currPlayer(value:PlayerObject):void {
			_currPlayer = value;
		}
		
		public function get handle():Handle {
			return _handle;
		}
		
		public function get socket():CustomSocket {
			return _socket;
		}
		
		public function get dailyObject():DailyObject {
			return _dailyObject;
		}
		
		public function set dailyObject(value:DailyObject):void {
			_dailyObject = value;
		}
		
		public function get config():ConfigObject {
			return _config;
		}
		
		public function get gameBoard():GameBoard 
		{
			return _gameBoard;
		}
		
		public function set gameBoard(value:GameBoard):void 
		{
			_gameBoard = value;
		}
		
		public function get dashBoard():DashBoard 
		{
			return _dashBoard;
		}
		
		public function get loaderFile():LoaderFile 
		{
			return _loaderFile;
		}
		
		public function get chatRoom():ChatRoom 
		{
			return _chatRoom;
		}
		
		public function set chatRoom(value:ChatRoom):void 
		{
			_chatRoom = value;
		}
		
		public function get sysSetting():SystemSetting 
		{
			return _sysSetting;
		}
		
		public function set sysSetting(value:SystemSetting):void 
		{
			_sysSetting = value;
		}
	
	}

}